/**
 * 
 */
package trungsi.gea.photos.client

import javax.swing.JPanel
import java.awt.Graphicsimport java.awt.BorderLayout as BLimport javax.swing.JComboBoximport javax.swing.DefaultComboBoxModelimport javax.swing.JLabelimport javax.swing.JListimport java.awt.Componentimport javax.swing.ListCellRendererimport javax.swing.JComponentimport java.awt.event.ActionListenerimport java.awt.GridLayoutimport javax.swing.JToggleButtonimport javax.swing.JScrollPaneimport javax.swing.DefaultListModelimport javax.swing.ImageIconimport javax.swing.SwingConstantsimport javax.swing.JButtonimport javax.swing.ScrollPaneConstantsimport java.awt.Dimensionimport java.awt.FlowLayoutimport javax.swing.JTextFieldimport java.awt.event.MouseAdapterimport java.awt.event.KeyAdapterimport java.awt.event.MouseListenerimport java.awt.event.MouseEvent
import groovy.swing.SwingBuilder

/**
 * @author trungsi
 *
 */
public class FoldersPanel extends JPanel {

	def service
	
	def selectedFolderComp
	
	def reloadButton
	def showPhotoButton
	def deleteButton
	
	def showPhotos
	
	def scrollPane
	def foldersPanel
	
	FoldersPanel(service) {
		this()
		setService(service)
	}
	
	public final void setService(service) {
		this.service = service
		loadFolders()
	}
	
	private void initUI() {
		this.setLayout(new BL())
		
		def buttonContainer = new JPanel(new FlowLayout(FlowLayout.LEFT))
		this.add(buttonContainer, BL.NORTH)
		
		reloadButton = new JButton('Reload')
		reloadButton.addActionListener({loadFolders()} as ActionListener)
		buttonContainer.add(reloadButton)
		
		showPhotoButton = new JButton('Show photos')
		showPhotoButton.addActionListener({
			if (selectedFolderComp)
				showPhotos?.call(selectedFolderComp.folder)
		} as ActionListener)
		buttonContainer.add(showPhotoButton)
		
		deleteButton = new JButton('Delete folder')
        deleteButton.addActionListener({deleteFolders()} as ActionListener)
        buttonContainer.add(deleteButton)
        
        
		foldersPanel = new JPanel(new ModifiedFlowLayout(FlowLayout.LEFT))
		scrollPane = new JScrollPane(foldersPanel)
		this.add(scrollPane)
	}
	
	FoldersPanel() {
		initUI()
	}
	
	void loadFolders() {
		SwingBuilder.build { 
			doOutside {
				def folders = service.getRemoteFolders()
				doLater {
					foldersPanel.removeAll()
					
					folders.each {folder ->
						foldersPanel.add(new FolderPanel(folder, this))
					}
					
					this.revalidate()
				}
			}
		}
	}
	
	void deleteFolders() {
		if (selectedFolderComp) {
			SwingBuilder.build { 
				doOutside {
					def result = service.deleteFolder(selectedFolderComp.folder.id)
					edt {
						if (result.success) {
							removeSelectedFolder()
						} else {
							// alert('failed')
						}
					}
				}
			}
		}
	}
	
	void removeSelectedFolder() {
		foldersPanel.remove(selectedFolderComp)
		foldersPanel.revalidate()
		selectedFolderComp = null
	}
	
	def getCoverUrl(folder) {
		service.getCoverUrl(folder.id)
	}
	
	def updateRemoteFolderName(folderId, folderName) {
		service.updateRemoteFolderName(folderId, folderName)
	}
}

class FolderPanel extends JPanel {
	def folder
	def container
	
	def imageContainer
	
	FolderPanel(folder, container) {
		this.folder = folder
		this.container = container
		
		initUI()
		loadImage()
	}
	
	private void loadImage() {
		SwingBuilder.build {
			doOutside {
				imageContainer.icon = new ImageIcon(container.getCoverUrl(folder))
				doLater {
					this.add(imageContainer, BL.CENTER)
					this.revalidate()
				}
			}
		}
	}
	
	private void initUI() {
		setLayout(new BL())
        add(new FolderLabel(folder, container), BL.SOUTH)
		
        imageContainer = new JToggleButton()
		imageContainer.verticalTextPosition = SwingConstants.BOTTOM
        imageContainer.horizontalTextPosition = SwingConstants.CENTER
        imageContainer.addActionListener({ev ->
	        if (ev.source.model.isSelected()) {
	            container.selectedFolderComp?.selected = false
	            container.selectedFolderComp = this
	        }
	    }as ActionListener)
	}
	
	void setSelected(bool) {
		imageContainer.selected = bool
	}
}

class FolderLabel extends JPanel {
	def folder
	def container
	
	def folderNameLabel
	def folderNameTextField
	
	FolderLabel(folder, container) {
		this.folder = folder
		this.container = container
		
		initUI()
	}
	
	private void initUI() {
		folderNameLabel = new JLabel(folder.name)
		folderNameLabel.addMouseListener([mouseClicked : {
			folderNameLabel.visible = false
			folderNameTextField.text = this.folder.name
			folderNameTextField.visible = true
			folderNameTextField.requestFocusInWindow()
			folderNameTextField.selectAll()
			
		}] as MouseAdapter)
		this.add(folderNameLabel)
		
		folderNameTextField = new JTextField(visible : false)
		folderNameTextField.addActionListener({
			if (folderNameTextField.text != this.folder.name) {
				SwingBuilder.build { 
		            doOutside {
		            	if (container.updateRemoteFolderName(this.folder.id, folderNameTextField.text)) {
		            		edt {
		            			this.folder.name = folderNameTextField.text
		            			this.folderNameLabel.text = folderNameTextField.text
		            			folderNameLabel.visible = true
		                        folderNameTextField.visible = false
		            		}
		            	} else {
		            		edt {
		            			folderNameLabel.visible = true
                                folderNameTextField.visible = false
		            		}
		            	}
		            }
				}
			} else {
				folderNameLabel.visible = true
				folderNameTextField.visible = false
			}
		} as ActionListener)
		this.add(folderNameTextField)
	}
}
